<?php
@session_start();
require_once('../config/config.php');
require_once('../utils/CommonUtils.php');
require_once('../utils/DBUtils.php');
include ("../utils/Constants.php");
require_once('../utils/BaseDAO.php');
require("../utils/Session.php");

$commonUtils = new CommonUtils();
$onlineUser = Session::get('fullname');
if($onlineUser == null){
	$commonUtils->redirect("../login.php"); 
}
$baseDAO = new BaseDAO();

$action = $commonUtils->getParameter("action");
$name = $commonUtils->getParameter("name");
$code = $commonUtils->getParameter("code");
$displayorder = $commonUtils->getParameter("displayorder");
$url = $commonUtils->getParameter("url");
$id = $commonUtils->getParameter("id");

if (isset($action) && $action == 'insert-update') {
	if(isset($name) && $name != "" &&
		isset($url) && $url != "" &&
		isset($code) && $code != ""){
	 		if(isset($id) && $id > 0){
	 			$item = $baseDAO->checkDuplicate("menu", "code", $code, $id);
	 			if($item > 0){
	 				$message = "Mã menu này đã tồn tại";
	 			}else{
		 			$values = array($id, $name, $code, $displayorder, $url);
		 			$retval = $baseDAO->update("menu", array("id", "name", "code", "displayorder", "url"), $values);
		 			$message = "Cập nhật thành công";
	 			}
	 		}else{
	 			$item = $baseDAO->checkDuplicate("menu", "code", $code);
	 			if($item > 0){
	 				$message = "Mã menu này đã tồn tại";
	 			}else{
		 			$values = array($name, $code, $displayorder, $url);
		 			$id = $retval = $baseDAO->create("menu", array("name", "code", "displayorder", "url"), $values);
		 			$message = "Thêm mới thành công";
	 			}
	 		}
				
	 	}else{
	 		$message = "Vui lòng nhập đủ các thông tin bắt buộc";
	 	}
}
if($id != null && $id > 0){
	$result = $baseDAO->getItem("menu", array("id", "name", "code", "displayorder", "url"), $id);
	if (!$result) {
	    echo 'Could not run query: ' . mysql_error();
	    exit;
	}
	$name = $result['name'];
	$code = $result['code'];
	$url = $result['url'];
	$displayorder = $result['displayorder'];
}
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=100" >
		<title>NguyenDau - Quản lý menu</title>
		<link rel="stylesheet" type="text/css" href="../css/diary.css"/>
	</head>
	<body>
		<?php include '../include/header.php';?>
		<center>
			<div class="content">
				<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="formlogin" >
					<div class="tableHeader">
						Quản lý menu
					</div>
					<?php if(isset($message)){?>
						<div class="infomessage"><?php echo $message; ?></div>
					<?php }?>
					<table>
						<tr>
							<td width="150px;">Mã menu<span class="required">*</span></td>
							<td><input type="text" name="code" value="<?php if(isset($code)){echo $code;}?>"/></td>
						</tr>
						<tr>
							<td>Tên menu<span class="required">*</span></td>
							<td><input type="text" name="name" value="<?php if(isset($name)){echo $name;}?>"/></td>
						</tr>
						<tr>
							<td>Url<span class="required">*</span></td>
							<td><input type="text" name="url" value="<?php if(isset($url)){echo $url;}?>"/></td>
						</tr>
						<tr>
							<td>Thứ tự</td>
							<td><input type="text" name="displayorder" value="<?php if(isset($displayorder)){echo $displayorder;}?>"/></td>
						</tr>
						<tr>
							<td></td>
							<td>
								<input type="submit" value="Đồng ý"/>
								<input type="button" value="Trở vể" onclick="window.location='./menu-list.php'"/>
							</td>
						</tr>
					</table>
					<input type="hidden" name="id" value="<?php if(isset($id)){echo $id;}?>"/>
					<input type="hidden" name="action" value="insert-update"> 
				</form>		
			</div>
		</center>
		<?php include '../include/footer.php';?>
	</body>
</html>

